package cloudPayAdmin.admin.controller.traffic;

import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cloudPayAdmin.admin.dbapp.entity.hicatcard.merchant.TMerchant;
import cloudPayAdmin.admin.dbapp.entity.traffic.TPicture;
import cloudPayAdmin.admin.dbapp.entity.traffic.TVideo;
import cloudPayAdmin.admin.service.traffic.PictureService;
import cloudPayAdmin.constant.Constant;
import cloudPayAdmin.util.AdminLteDataTableResp;
import cloudPayAdmin.util.AdminLteUtil;
import cloudPayAdmin.util.AliyunUtil;
import cloudPayAdmin.util.ImageUtils;
import cloudPayAdmin.util.TimeUtil;
import cloudPayAdmin.util.WebUtil;
import cloudPayAdmin.util.pagebean.HqlPageBeanHelper;
import cloudPayAdmin.util.pagebean.HqlParam;
import cloudPayAdmin.util.pagebean.PageBean;

@Controller
@RequestMapping("/traffic/picture")
public class PictureController {
	
	@Autowired
	HqlPageBeanHelper hqlPageBeanHelper;

	@PersistenceContext(unitName = "entityManagerFactory")
	EntityManager entityManager;
	
	@Autowired
	Environment env;
	
	@Autowired
	PictureService pictureService;
	
	@RequestMapping(value = "/toListPage")
	public String toListPage(HttpServletRequest request, Map<String, Object> model) {
		request.setAttribute("typeMap", TPicture.initTypeMap());
		return "traffic/picture/picture-list";
	}
	
	@RequestMapping("/getPictureListData")
	@ResponseBody
	public String getPictureListData(HttpServletRequest request) {
		Map<String, Object> paramMap = WebUtil.getSearchParam(request);
		paramMap = WebUtil.formatDateRange(paramMap, TimeUtil.DATE_PATTERN_NOSEPARTOR);
		// 需要映射的实体类
		HqlParam hqlParam = WebUtil.buildHqlParam(paramMap, TPicture.class, request);
		PageBean<TPicture> cloudpayCardPage = hqlPageBeanHelper.findHqlPageBean(entityManager, hqlParam);
		
		for(TPicture picture : cloudpayCardPage.getContent()) {
			if(StringUtils.isNotBlank(picture.getUrl())) {
				picture.setUrl(AliyunUtil.getPublicUrl(AliyunUtil.PUBLIC_BUCKETNAME, picture.getUrl(), Boolean.FALSE));
			}			
		}
		AdminLteDataTableResp<TPicture> resp = AdminLteUtil.pageBeanToAdminLteDataTableResp(cloudpayCardPage,request);
		return resp.toAdminLteJsonString(TPicture.class);
	}
	
	@RequestMapping("/toPictureEditPage")
	public String toPictureEditPage(HttpServletRequest request,Long pictureId) {
		TPicture picture = new TPicture();
		if(pictureId != null) {
			TPicture picture2 = pictureService.findById(pictureId);
			if(picture2 != null) {
				picture = picture2;
			}
			
		}
		
		picture.setUrl(AliyunUtil.getPublicUrl(AliyunUtil.PUBLIC_BUCKETNAME, picture.getUrl(), Boolean.FALSE));
		
		request.setAttribute("picture", picture);
		request.setAttribute("typeMap", TPicture.initTypeMap());
		
		return "traffic/picture/picture-edit";
	}
	
	@RequestMapping("/editPicture")
	public String editPicture(HttpServletRequest request,TPicture picture, MultipartFile picturePic) throws Exception {
		
		if(picture.getPictureId() == null) {   //新增
			
			if(picturePic != null && StringUtils.isNotBlank(picturePic.getOriginalFilename())) {
				String key = AliyunUtil.generateAliyunKey(Constant.PICTURE_FILE_PATH, picturePic.getOriginalFilename());
				AliyunUtil.uploadFile(AliyunUtil.PUBLIC_BUCKETNAME, picturePic.getInputStream(), key);
				picture.setUrl(key); 
				
				int[] widthAndHeight = ImageUtils.getSizeInfo(picturePic.getInputStream());
				picture.setWidth(widthAndHeight[0]);
				picture.setHeight(widthAndHeight[1]);
			}
			
			picture.setType(TPicture.TYPE_UPLOAD);
			picture.setCreateTime(TimeUtil.getCurrTime(TimeUtil.DATE_PATTERN_NOSEPARTOR));
			picture.setUpdateTime(TimeUtil.getCurrTime(TimeUtil.DATE_PATTERN_NOSEPARTOR));
			pictureService.save(picture);
		} else {    //更新
			TPicture oriPicture = pictureService.findById(picture.getPictureId());
			
			if(picturePic != null && StringUtils.isNotBlank(picturePic.getOriginalFilename())) {
				String key = AliyunUtil.generateAliyunKey(Constant.PICTURE_FILE_PATH, picturePic.getOriginalFilename());
				AliyunUtil.uploadFile(AliyunUtil.PUBLIC_BUCKETNAME, picturePic.getInputStream(), key);
				oriPicture.setUrl(key); 
				
				int[] widthAndHeight = ImageUtils.getSizeInfo(picturePic.getInputStream());
				oriPicture.setWidth(widthAndHeight[0]);
				oriPicture.setHeight(widthAndHeight[1]);
			}
			
			oriPicture.setPictureName(picture.getPictureName());
			oriPicture.setTag(picture.getTag());
			oriPicture.setUpdateTime(TimeUtil.getCurrTime(TimeUtil.DATE_PATTERN_NOSEPARTOR));
			pictureService.save(oriPicture);
		}
		
		return "redirect:/traffic/picture/toListPage";
	}
}
